package com.example.controller;

import com.example.common.Result;
import com.example.entity.Member;
import com.example.service.MemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 普通用户信息表(Member)表控制层
 *
 */
@CrossOrigin//解决跨域问题注解
@RestController
public class MemberController {
    @Resource
    private MemberService memberService;
    
    //用户登录
    @ResponseBody
    @RequestMapping("memberLogin")
    public Result Login(Member member, HttpServletRequest request, HashMap<String, Object> map) {
        Member loggedMember = memberService.login(member, request, map);
        return Result.success(loggedMember);
    }

    //用户注册
    @ResponseBody
    @RequestMapping("memberReg")
    public Result memberReg(Member member, HttpServletRequest request, HashMap<String, Object> map) {
        memberService.register(member, map);
        return Result.success();
    }

    //信息列表
    @ResponseBody
    @RequestMapping("memberList")
    public Result memberList(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "1", value = "pageSize") Integer pageSize, HttpServletRequest request) {
        String key = request.getParameter("key");
        String key1 = request.getParameter("key1");
        
        PageInfo<Member> pageInfo = memberService.getMemberList(pageNum, pageSize, key, key1);
        return Result.success(pageInfo);
    }

    //添加或编辑信息
    @ResponseBody
    @RequestMapping("memberAdd")
    public Result memberAdd(Member member, HttpServletRequest request) {
        memberService.saveOrUpdate(member, request);
        return Result.success();
    }

    //账户冻结及解冻
    @ResponseBody
    @RequestMapping("memberEdit")
    public Result memberEdit(Member member, HttpServletRequest request) {
        Member updatedMember = memberService.updateMemberStatus(member);
        return Result.success(updatedMember);
    }

    //个人密码编辑
    @ResponseBody
    @RequestMapping("memberPass")
    public Result memberPass(Member member, HttpServletRequest request) {
        String onlpassword = request.getParameter("onlpassword");
        String confirmPasword = request.getParameter("confirmPasword");
        
        memberService.updatePassword(member, onlpassword, confirmPasword);
        return Result.success();
    }

    //删除信息
    @ResponseBody
    @RequestMapping("memberDel")
    public Result memberDel(int id, HttpServletRequest request) {
        memberService.deleteMember(id);
        return Result.success();
    }

    //批量删除信息
    @ResponseBody
    @RequestMapping("memberDeleteAll")
    public Result typeDeleteAll(HttpServletRequest request) {
        String vals = request.getParameter("vals");
        memberService.batchDeleteMember(vals);
        return Result.success();
    }
}

